/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** *****************************************************************************************************
 *  \file     Ea.h                                                                                      *
 *  \brief    Semidrive. AUTOSAR 4.3.1 MCAL EEPROM Abstraction                                          *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/11/13     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/
#ifndef EA_H
#define EA_H

#ifdef __cplusplus
extern "C" {
#endif

/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Compiler.h"
#include "Compiler_Cfg.h"
#include "Std_Types.h"
#include "MemIf_Types.h"
#include "Ea_Types.h"
#include "Ea_Cfg.h"

/********************************************************************************************************
 *                                 Global Macro definition                                              *
 *******************************************************************************************************/
/*****************Version Information************/
#define EA_VENDOR_ID                        140U
#define EA_MODULE_ID                        40U
#define EA_INSTANCE_ID                      0U

#define EA_AR_MAJOR_VERSION                 4U
#define EA_AR_MINOR_VERSION                 3U
#define EA_AR_PATCH_VERSION                 1U

#define EA_SW_MAJOR_VERSION                 1U
#define EA_SW_MINOR_VERSION                 0U
#define EA_SW_PATCH_VERSION                 0U

/* Service id's for Ea functions */
#define EA_INIT_ID                          0x00U
#define EA_SET_MODE_ID                      0x01U
#define EA_READ_ID                          0x02U
#define EA_WRITE_ID                         0x03U
#define EA_CANCEL_ID                        0x04U
#define EA_GETSTATUS_ID                     0x05U
#define EA_GETJOBRESULT_ID                  0x06U
#define EA_INVALIDATEBLOCK_ID               0x07U
#define EA_GETVERSIONINFO_ID                0x08U
#define EA_ERASEIMMEDIATE_BLOCK_ID          0x09U
#define EA_JOBENDNOTIFICATION_ID            0x10U
#define EA_JOBERRORNOTIFICATION_ID          0x11U
#define EA_MAINFUNCTION_ID                  0x12U

/*Service ID for DET reporting */
/* Development Errors */
#define EA_E_UNINIT                         0x01U
#define EA_E_INVALID_BLOCK_NO               0x02U
#define EA_E_INVALID_BLOCK_OFS              0x03U
#define EA_E_PARAM_POINTER                  0x04U
#define EA_E_INVALID_BLOCK_LEN              0x05U
#define EA_E_INIT_FAILED                    0x09U

/* Runtime Errors */
#define EA_E_BUSY                           0x06U
#define EA_E_INVALID_CANCEL                 0x08U

/* Production Errors */
#define EA_E_VENDOR                         0x10U
#define EA_E_INTERNAL_BUSY                  ((EA_E_VENDOR) | 0x0U)

#define EA_BLOCK_INDEX_DEF                  0xFFFFU /* default block index value */

/********************************************************************************************************
 *                                  Global Types definition                                             *
 *******************************************************************************************************/

/* Job state */
typedef enum {
    EA_JOB_READ = 0,                        /* read the EA block data */
    EA_JOB_READ_DATA,

    EA_JOB_WRITE,                           /* write the EA block data */
    EA_JOB_WRITE_BACKUP_RECORD_INFO,        /* sync EA blcok to backup block */
    EA_JOB_WRITE_READ_BACKUP_RECORD_DATA,
    EA_JOB_WRITE_SYNC_BACKUP_RECORD_DATA,
    EA_JOB_WRITE_BACKUP_RECORD_ACTIVE,

    EA_JOB_WRITE_MOVE_BACKUP_RECORD_INFO,   /* move backup block info */
    EA_JOB_WRITE_MOVE_READ_BACKUP_RECORD_DATA,
    EA_JOB_WRITE_MOVE_SYNC_BACKUP_RECORD_DATA,
    EA_JOB_WRITE_MOVE_BACKUP_RECORD_ACTIVE,

    EA_JOB_WRITE_BLOCK_INFO_END,            /* move block info end flag */
    EA_JOB_WRITE_RECORD_INFO,
    EA_JOB_WRITE_BLOCK_DATA_END,            /* move block data end flag */
    EA_JOB_WRITE_RECORD_DATA,
    EA_JOB_WRITE_RECORD_ACTIVE,

    EA_JOB_INVAL_BLOCK,                    /* set EA block invalid */
    EA_JOB_INVAL_BLOCK_INFO,

    EA_JOB_ERASE_IMMEDIATE,                /* verify that the immediate write space is free */

    EA_JOB_DONE,                           /* EA JOB complete */

    EA_JOB_MAX,
} Ea_JobType;

typedef enum {
    EA_INTERNAL_UNINIT = 0,                           /* Ea Internal not init */
    EA_INTERNAL_INIT,                                 /* Ea Internal init */
    EA_INTERNAL_IDLE,                                 /* Ea Internal idle */
    EA_INTERNAL_BUSY,                                 /* Ea Internal busy */
    EA_INTERNAL_FAILED,                               /* Ea Internal failed */
} Ea_Internal_StatusType;

typedef enum {
    EA_INTERNAL_JOB_OK = 0,                               /* Ea Internal job has been finished successfully */
    EA_INTERNAL_JOB_FAILED,                           /* Ea Internal job has not been finished successfully */
    EA_INTERNAL_JOB_PENDING,                          /* Ea Internal job has not yet been finished */
} Ea_Internal_JobResultType;

typedef enum {
    EA_INTERNAL_JOB_INT = 0,
    EA_INTERNAL_JOB_INT_GET_DEV_INFO,
    EA_INTERNAL_JOB_INT_PARES_DEV_INFO,
    EA_INTERNAL_JOB_INT_SET_DEV_INFO,
    EA_INTERNAL_JOB_INT_SET_BLOCK_DATA_END,
    EA_INTERNAL_JOB_INT_SET_BLOCK_INFO_END,
    EA_INTERNAL_JOB_INT_GET_BLOCK_INFO,
    EA_INTERNAL_JOB_INT_PARES_BLOCK_INFO,
    EA_INTERNAL_JOB_INT_BLOCK_INFO_LOOP,
    EA_INTERNAL_JOB_INT_GET_BLOCK_DATA_END,
    EA_INTERNAL_JOB_INT_PARES_BLOCK_DATA_END,
    EA_INTERNAL_JOB_INT_GET_BACKUP_BLOCK_INFO,
    EA_INTERNAL_JOB_INT_PARES_BACKUP_BLOCK_INFO,
    EA_INTERNAL_JOB_INT_READ_BACKUP_BLOCK_DATA,
    EA_INTERNAL_JOB_INT_SYNC_BACKUP_BLOCK_DATA,
    EA_INTERNAL_JOB_INT_BACKUP_BLOCK_ACTIVE,
    EA_INTERNAL_JOB_INT_DONE,
    EA_INTERNAL_JOB_DONE,
    EA_INTERNAL_JOB_MAX,
} Ea_Internal_JobType;

typedef struct {
    MemIf_StatusType Ea_eModuleStatus;      /* ea module status */
    MemIf_JobResultType Ea_eJobResult;      /* ea job result */
    Ea_JobType Ea_eJob;                     /* ea job status */
    Ea_JobType Ea_eEepJob;                  /* ea job calling eep job */
    uint16 Ea_uJobBlockIndex;
    uint16 Ea_uJobBlockNumber;
    uint16 Ea_uJobBlockOffset;
    uint16 Ea_uJobBlockLength;
    uint8 *Ea_pJobReadDataDestPtr;
    const uint8 *Ea_pJobWriteDataDestPtr;

    Ea_Internal_StatusType Ea_eInternalStatus;      /* ea Internal status */
    Ea_Internal_JobResultType
    Ea_eInternalJobResult;                          /* ea Internal job result */
    Ea_Internal_JobType
    Ea_eInternalJob;                                /* ea Internal job status */
    Ea_Internal_JobType
    Ea_eInternalEepJob;                             /* ea Internal job calling eep job */

    uint16 Ea_uJobIntDevIt;
    Eep_LengthType Ea_uJobIntDevOffset;
    Eep_LengthType Ea_uJobIntDevSize;
    Eep_LengthType Ea_uJobIntRecordIndex;

    uint16 Ea_uBackupBlockIndex;
    Eep_LengthType Ea_uJobDataOffset;
    Eep_LengthType Ea_uJobDataSize;
    Eep_LengthType Ea_uJobMoveDataOffset;
} Ea_StateType;

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
/******************************************************************************
** Service name        : Ea_Init                                                **
** Parameters (in)     : ConfigPtr  Pointer to block configuration set          **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description        : Initializes the EEPROM abstraction module               **
******************************************************************************/
extern void Ea_Init(const Ea_ConfigType *ConfigPtr );

/******************************************************************************
** Service name        : Ea_SetMode                                             **
** Parameters (in)     : MemIf_ModeType Mode (Mode MEMIF_MODE_SLOW /            **
**                                         MEMIF_MODE_FAST)                     **
** Parameters (out)    : None                                                   **
** Return value        : void                                                   **
** Description        : Call the Eep_SetMode function                           **
******************************************************************************/
#if (EA_SETMODE_API_SUPPORTED == STD_ON)
extern void Ea_SetMode( MemIf_ModeType Mode );
#endif

/******************************************************************************
** Service name        : Ea_Read                                                **
** Parameters (in)     : uint16 BlockNumber :block number                       **
**                      uint16 BlockOffset : block addr offset                  **
**                      uint8* DataBufferPtr :Pointer to data buffer            **
**                      uint16 Length : Number of bytes to read                 **
** Parameters (out)    : None                                                   **
** Return value        : Std_ReturnType ( E_OK / E_NOT_OK )                     **
** Description        : Reads from ea block                                     **
******************************************************************************/
extern Std_ReturnType Ea_Read( uint16 BlockNumber,
                               uint16 BlockOffset,
                               uint8 *DataBufferPtr,
                               uint16 Length );


/******************************************************************************
** Service name        : Ea_Write                                               **
** Parameters (in)     : uint16 BlockNumber :block number                       **
**                      const uint8* DataBufferPtr :Pointer to data buffer      **
** Parameters (out)    : None                                                   **
** Return value        : Std_ReturnType ( E_OK / E_NOT_OK )                     **
** Description        : Writes to ea block                                      **
******************************************************************************/
extern Std_ReturnType Ea_Write( uint16 BlockNumber,
                                const uint8 *DataBufferPtr );


/******************************************************************************
** Service name        : Ea_Cancel                                              **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description        : Cancels an ongoing job                                  **
******************************************************************************/
extern void Ea_Cancel( void );

/******************************************************************************
** Service name        : Ea_GetStatus                                           **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : MemIf_StatusType                                       **
** Description        : Returns the ea state                                    **
******************************************************************************/
extern MemIf_StatusType Ea_GetStatus( void );

/******************************************************************************
** Service name        : Ea_GetJobResult                                        **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : MemIf_JobResultType                                    **
** Description        : Returns the result of the last job                      **
******************************************************************************/
extern MemIf_JobResultType Ea_GetJobResult( void );

/******************************************************************************
** Service name        : Ea_GetJobType                                          **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : Ea_JobType                                             **
** Description        : Returns the last job type                               **
******************************************************************************/
extern Ea_JobType Ea_GetJobType( void );

/******************************************************************************
** Service name        : EaIntl_GetStatus                                       **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : Ea_Internal_StatusType                                 **
** Description        : Returns the ea internal state                           **
******************************************************************************/
extern Ea_Internal_StatusType EaIntl_GetStatus( void );

/******************************************************************************
** Service name        : EaIntl_GetJobResult                                    **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : Ea_Internal_JobResultType                              **
** Description        : Returns the result of the last internal job             **
******************************************************************************/
extern Ea_Internal_JobResultType EaIntl_GetJobResult( void );

/******************************************************************************
** Service name        : EaIntl_GetJobType                                      **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : Ea_Internal_JobType                                    **
** Description        : Returns the last internal job type                      **
******************************************************************************/
extern Ea_Internal_JobType EaIntl_GetJobType( void );

/******************************************************************************
** Service name        : Ea_InvalidateBlock                                     **
** Parameters (in)     : uint16 BlockNumber :block number                       **
** Parameters (out)    : None                                                   **
** Return value        : Std_ReturnType ( E_OK / E_NOT_OK )                     **
** Description         : set ea block invalid                                   **
******************************************************************************/
extern Std_ReturnType Ea_InvalidateBlock( uint16 BlockNumber );

/******************************************************************************
** Service name        : Ea_GetVersionInfo                                      **
** Parameters (in)     : Std_VersionInfoType VersionInfoPtr :                   **
**                          Pointer to VersionInfo buffer                       **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description         : get ea VersionInfo                                     **
******************************************************************************/
#if (EA_VERSION_INFO_API == STD_ON)
extern void Ea_GetVersionInfo( Std_VersionInfoType *VersionInfoPtr );
#endif

/******************************************************************************
** Service name        : Ea_EraseImmediateBlock                                 **
** Parameters (in)     : uint16 BlockNumber :block number                       **
** Parameters (out)    : None                                                   **
** Return value        : Std_ReturnType ( E_OK / E_NOT_OK )                     **
** Description         : ea erase immediate block                               **
******************************************************************************/
extern Std_ReturnType Ea_EraseImmediateBlock( uint16 BlockNumber );

/******************************************************************************
** Service name        : Ea_JobEndNotification                                  **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description        : return the end result of jobs                           **
******************************************************************************/
extern void Ea_JobEndNotification( void );

/******************************************************************************
** Service name        : Ea_JobErrorNotification                                **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description        : notice the error result of jobs                         **
******************************************************************************/
extern void Ea_JobErrorNotification( void );

/******************************************************************************
** Service name        : Ea_MainFunction                                        **
** Parameters (in)     : None                                                   **
** Parameters (out)    : None                                                   **
** Return value        : None                                                   **
** Description        : Performs the processing of jobs                         **
******************************************************************************/
extern void Ea_MainFunction( void );

#ifdef __cplusplus
}
#endif

#endif /* EA_H */

/* End of file */
